package me.pearjelly.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import me.pearjelly.model.PageContext;
import edu.uci.ics.crawler4j.crawler.Page;

public class CloneUtil {
	public static Object clone(Object obj) {
		try {
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bo);
			oos.writeObject(obj);
			ByteArrayInputStream inputStream = new ByteArrayInputStream(
					bo.toByteArray());
			return new ObjectInputStream(inputStream).readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		Page page = new Page(null);
		PageContext obj = new PageContext(page);
		System.out.println(obj);
		PageContext obj2 = (PageContext)CloneUtil.clone(obj);
		System.out.println(obj2);
	}
}
